﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.DB.Plumbing;
using YArchitech.LIB;
using YArchitech.MEP;
using YArchitech.Revit;
using YArchitech.Revit.RevitExtension;

namespace YArchitech.HVAC
{
	public class OpearComprhsivPipelineSec
	{
		public OpearComprhsivPipelineSec()
		{
		}

		public OpearComprhsivPipelineSec(Autodesk.Revit.DB.Document document)
		{
			this.doc = document;
			this.revitVer = RevitVersion.GetVersionNumber(this.doc);
			this.outerDistance = 1.5 * this.outerDistance;
		}

		public void Execute()
		{
			this.tubeSynthesize = this.userSetting.GetTubeSynthesize();
			this.deleteDimensions();
			List<Element> elemsOfComprhsivPipeline = this.GetElemsOfComprhsivPipeline(this.doc, true);
			this.RemoveOnLineElements(ref elemsOfComprhsivPipeline);
			List<Element> elemsOfComprhsivPipeline2 = this.GetElemsOfComprhsivPipeline(this.doc, false);
			if (elemsOfComprhsivPipeline.Count > 0)
			{
				XYZ point;
				XYZ point2;
				XYZ center = this.CreateElemsSysTypeAndSizeTagging(this.doc, elemsOfComprhsivPipeline, elemsOfComprhsivPipeline2, out point, out point2);
				List<Element> list = new List<Element>();
				list.AddRange(elemsOfComprhsivPipeline);
				list.AddRange(elemsOfComprhsivPipeline2);
				ElementSort elementSort = new ElementSort(this.doc);
				List<Element> operateElems = this.GetOperateElems(elemsOfComprhsivPipeline);
				this.SetRowDimension(elementSort.RowSort(operateElems), elementSort, list, elemsOfComprhsivPipeline2);
				if (!this.tubeSynthesize.BeOffset)
				{
					this.SetColDimension(elementSort.ColSort(operateElems, true, center), true, elementSort, elemsOfComprhsivPipeline2, point);
					this.SetColDimension(elementSort.ColSort(operateElems, false, center), false, elementSort, elemsOfComprhsivPipeline2, point2);
				}
			}
		}

		private void deleteDimensions()
		{
			List<ElementId> list = new FilteredElementCollector(this.doc, this.doc.ActiveView.Id).OfCategory(BuiltInCategory.OST_Dimensions).ToElementIds().ToList<ElementId>();
			this.doc.Delete(list);
			foreach (Element element in new FilteredElementCollector(this.doc, this.doc.ActiveView.Id).OfClass(typeof(CurveElement)).ToElements().ToList<Element>())
			{
				if (element is DetailLine && ((DetailLine)element).LineStyle.Name == "HY-CompLine")
				{
					this.doc.DeleteElement(element);
				}
			}
			List<ElementId> list2 = new FilteredElementCollector(this.doc, this.doc.ActiveView.Id).OfCategory(BuiltInCategory.OST_TextNotes).ToElementIds().ToList<ElementId>();
			this.doc.Delete(list2);
		}

		private void RemoveOnLineElements(ref List<Element> operateElems)
		{
			if (operateElems == null || operateElems.Count < 1)
			{
				return;
			}
			List<Element> list = new List<Element>();
			for (int i = 0; i < operateElems.Count; i++)
			{
				HYLine elementLocationCurve = this.GetElementLocationCurve(operateElems[i]);
				if (elementLocationCurve == null)
				{
					operateElems.RemoveAt(i);
				}
				else
				{
					list.Add(operateElems[i]);
					for (int j = i + 1; j < operateElems.Count; j++)
					{
						HYLine elementLocationCurve2 = this.GetElementLocationCurve(operateElems[j]);
						if (elementLocationCurve2 == null)
						{
							operateElems.RemoveAt(j);
							j--;
						}
						else if (elementLocationCurve.IsLineOnLine(elementLocationCurve2.Line))
						{
							operateElems.RemoveAt(j);
							j--;
						}
					}
				}
			}
			operateElems = list;
		}

		private HYLine GetElementLocationCurve(Element elem)
		{
			LocationCurve locationCurve = elem.Location as LocationCurve;
			if (locationCurve == null)
			{
				return null;
			}
			return new HYLine(locationCurve.Curve.GetEndPoint(0), locationCurve.Curve.GetEndPoint(1));
		}

		private List<Element> GetOperateElems(List<Element> elems)
		{
			List<Element> list = new List<Element>();
			Dictionary<Element, XYZ> positionInSection = this.GetPositionInSection(this.doc, elems);
			Dictionary<Element, XYZ> dictionary = new Dictionary<Element, XYZ>();
			using (Dictionary<Element, XYZ>.Enumerator enumerator = positionInSection.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					KeyValuePair<Element, XYZ> item = enumerator.Current;
					if (!(from s in dictionary
					where s.Value.IsAlmostEqualTo(item.Value, 0.0001)
					select s).Any<KeyValuePair<Element, XYZ>>())
					{
						dictionary.Add(item.Key, item.Value);
						list.Add(item.Key);
					}
				}
			}
			return list;
		}

		private bool SetColDimension(List<List<Element>> listRow, bool isLeft, ElementSort sort, List<Element> dielems, XYZ point)
		{
			this.listVerticalRefernce = new List<ReferencePlane>();
			ReferenceArray referenceArray = new ReferenceArray();
			List<double> xyzList = new List<double>();
			HYLine hyline = null;
			HYLine hyline2 = null;
			if (point.IsAlmostEqualTo(new XYZ(0.0, 0.0, 0.0)))
			{
				point = this.doc.ActiveView.Origin;
			}
			if (isLeft)
			{
				hyline = new HYLine(point + -NumbericTools.mmToFeet(this.innerDistance) * this.doc.ActiveView.RightDirection, point + -NumbericTools.mmToFeet(this.innerDistance) * this.doc.ActiveView.RightDirection + this.doc.ActiveView.UpDirection);
				hyline2 = new HYLine(point + -(NumbericTools.mmToFeet(this.innerDistance) + NumbericTools.mmToFeet(this.outerDistance) * (double)this.doc.ActiveView.Scale / 100.0) * this.doc.ActiveView.RightDirection, point + -(NumbericTools.mmToFeet(this.innerDistance) + NumbericTools.mmToFeet(this.outerDistance) * (double)this.doc.ActiveView.Scale / 100.0) * this.doc.ActiveView.RightDirection + this.doc.ActiveView.UpDirection);
			}
			else
			{
				hyline = new HYLine(point + NumbericTools.mmToFeet(this.innerDistance) * this.doc.ActiveView.RightDirection, point + NumbericTools.mmToFeet(this.innerDistance) * this.doc.ActiveView.RightDirection + this.doc.ActiveView.UpDirection);
				hyline2 = new HYLine(point + (NumbericTools.mmToFeet(this.innerDistance) + NumbericTools.mmToFeet(this.outerDistance) * (double)this.doc.ActiveView.Scale / 100.0) * this.doc.ActiveView.RightDirection, point + (NumbericTools.mmToFeet(this.innerDistance) + NumbericTools.mmToFeet(this.outerDistance) * (double)this.doc.ActiveView.Scale / 100.0) * this.doc.ActiveView.RightDirection + this.doc.ActiveView.UpDirection);
			}
			foreach (List<Element> list in listRow)
			{
				if (list != null && list.Count<Element>() > 0)
				{
					list.Reverse();
				}
				using (List<Element>.Enumerator enumerator2 = list.GetEnumerator())
				{
					while (enumerator2.MoveNext())
					{
						Element element = enumerator2.Current;
						if (element is Pipe)
						{
							if (this.tubeSynthesize.Pipe)
							{
								this.AddPointReference(hyline, element, sort, referenceArray, false, xyzList);
							}
							else
							{
								this.AddEgdeReference(hyline, element, sort, referenceArray, false, xyzList);
							}
						}
						else if (element is Conduit)
						{
							if (this.tubeSynthesize.Conduit)
							{
								this.AddPointReference(hyline, element, sort, referenceArray, false, xyzList);
							}
							else
							{
								this.AddEgdeReference(hyline, element, sort, referenceArray, false, xyzList);
							}
						}
						else if (element is Duct)
						{
							if (this.tubeSynthesize.Duct)
							{
								this.AddPointReference(hyline, element, sort, referenceArray, false, xyzList);
							}
							else
							{
								this.AddEgdeReference(hyline, element, sort, referenceArray, false, xyzList);
							}
						}
						else if (element is CableTray)
						{
							if (this.tubeSynthesize.CableTray)
							{
								this.AddPointReference(hyline, element, sort, referenceArray, false, xyzList);
							}
							else
							{
								this.AddEgdeReference(hyline, element, sort, referenceArray, false, xyzList);
							}
						}
					}
					break;
				}
			}
			ReferenceArray referenceArray2 = new ReferenceArray();
			this.listVerticalRefernce = this.RemoveOverLapReference(this.listVerticalRefernce);
			referenceArray = this.GetReferenceArrayByPlane(this.listVerticalRefernce);
			foreach (Reference reference in sort.GetUpAndDownReference(dielems))
			{
				referenceArray.Append(reference);
				referenceArray2.Append(reference);
			}
			if (referenceArray.Size < 2)
			{
				return false;
			}
			this.doc.Regenerate();
			Dimension dimension = this.doc.Create.NewDimension(this.doc.ActiveView, hyline.Line, referenceArray, this.GetDimTypeByName());
			RevitDimensionExtension.ResetDimTextPos(dimension);
			this.SetDimesionType(dimension);
			if (referenceArray2.Size >= 1)
			{
				try
				{
					Dimension dimension2 = this.doc.Create.NewDimension(this.doc.ActiveView, hyline2.Line, referenceArray2, this.GetDimTypeByName());
					RevitDimensionExtension.ResetDimTextPos(dimension2);
					this.SetDimesionType(dimension2);
				}
				catch (Exception)
				{
				}
			}
			this.doc.Regenerate();
			return true;
		}

		private List<ReferencePlane> RemoveOverLapReference(List<ReferencePlane> originalPlanes)
		{
			List<ReferencePlane> list = new List<ReferencePlane>();
			if (originalPlanes == null || originalPlanes.Count < 2)
			{
				return originalPlanes;
			}
			HYLine hylineByReference = this.GetHYLineByReference(originalPlanes[0]);
			if (hylineByReference == null)
			{
				return originalPlanes;
			}
			for (int i = 0; i < originalPlanes.Count; i++)
			{
				list.Add(originalPlanes[i]);
				XYZ project = hylineByReference.GetProject(originalPlanes[i].FreeEnd);
				for (int j = i + 1; j < originalPlanes.Count; j++)
				{
					XYZ project2 = hylineByReference.GetProject(originalPlanes[j].FreeEnd);
					if (project.DistanceTo(project2) < 0.0032808398950131233)
					{
						originalPlanes.RemoveAt(j);
						j--;
					}
				}
			}
			return list;
		}

		private ReferenceArray GetReferenceArrayByPlane(List<ReferencePlane> listPlane)
		{
			ReferenceArray referenceArray = new ReferenceArray();
			if (listPlane == null || listPlane.Count < 1)
			{
				return referenceArray;
			}
			foreach (ReferencePlane rPlane in listPlane)
			{
				referenceArray.Append(RevitVersionFuncs.GetReference(rPlane));
			}
			return referenceArray;
		}

		private HYLine GetHYLineByReference(ReferencePlane rPlane)
		{
			HYLine hyline = null;
			if (rPlane == null)
			{
				return hyline;
			}
			XYZ normal = rPlane.Normal;
			hyline = new HYLine(rPlane.FreeEnd, rPlane.FreeEnd.Add(normal));
			hyline.MakeUnBound();
			return hyline;
		}

		private bool SetRowDimension(List<List<Element>> listRow, ElementSort sort, List<Element> eles, List<Element> dielems)
		{
			ReferenceArray referenceArray = new ReferenceArray();
			XYZ directionInstance = sort.GetDirectionInstance(dielems, this.doc.ActiveView.UpDirection, true);
			if (directionInstance == null)
			{
				return false;
			}
			HYLine hyline = new HYLine(new XYZ(directionInstance.X, directionInstance.Y, directionInstance.Z + NumbericTools.mmToFeet(this.innerDistance)), new XYZ(directionInstance.X, directionInstance.Y, directionInstance.Z + NumbericTools.mmToFeet(this.innerDistance)) + this.doc.ActiveView.RightDirection);
			HYLine hyline2 = new HYLine(new XYZ(directionInstance.X, directionInstance.Y, directionInstance.Z + NumbericTools.mmToFeet(this.innerDistance) + NumbericTools.mmToFeet(this.outerDistance)), new XYZ(directionInstance.X, directionInstance.Y, directionInstance.Z + NumbericTools.mmToFeet(this.innerDistance) + NumbericTools.mmToFeet(this.outerDistance)) + this.doc.ActiveView.RightDirection);
			bool flag = true;
			foreach (List<Element> list in listRow)
			{
				foreach (Element element in list)
				{
					if (element is Pipe)
					{
						if (this.tubeSynthesize.Pipe)
						{
							this.AddPointReference(hyline, element, sort, referenceArray, true, null);
						}
						else if (flag)
						{
							this.AddEgdeReference(hyline, element, sort, referenceArray, true, null);
						}
					}
					else if (element is Conduit)
					{
						if (this.tubeSynthesize.Conduit)
						{
							this.AddPointReference(hyline, element, sort, referenceArray, true, null);
						}
						else if (flag)
						{
							this.AddEgdeReference(hyline, element, sort, referenceArray, true, null);
						}
					}
					else if (element is Duct)
					{
						if (this.tubeSynthesize.Duct)
						{
							this.AddPointReference(hyline, element, sort, referenceArray, true, null);
						}
						else
						{
							this.AddEgdeReference(hyline, element, sort, referenceArray, true, null);
						}
					}
					else if (element is CableTray)
					{
						if (this.tubeSynthesize.CableTray)
						{
							this.AddPointReference(hyline, element, sort, referenceArray, true, null);
						}
						else
						{
							this.AddEgdeReference(hyline, element, sort, referenceArray, true, null);
						}
					}
				}
				flag = false;
			}
			ReferenceArray referenceArray2 = new ReferenceArray();
			List<Reference> leftAndRightReference = sort.GetLeftAndRightReference(eles);
			if (leftAndRightReference != null)
			{
				foreach (Reference reference in leftAndRightReference)
				{
					referenceArray.Append(reference);
				}
			}
			this.listHorizontalReference = this.RemoveOverLapReference(this.listHorizontalReference);
			List<Autodesk.Revit.DB.View> floorPlanViews = this.GetFloorPlanViews(this.doc);
			foreach (ReferencePlane referencePlane in this.listHorizontalReference)
			{
				ElementSet elementSet = new ElementSet();
				elementSet.Insert(referencePlane);
				foreach (Autodesk.Revit.DB.View view in floorPlanViews)
				{
					view.HideYJKElements(elementSet);
				}
			}
			ReferenceArray referenceArrayByPlane = this.GetReferenceArrayByPlane(this.listHorizontalReference);
			if (referenceArrayByPlane != null & referenceArrayByPlane.Size > 0)
			{
				foreach (object obj in referenceArrayByPlane)
				{
					Reference reference2 = (Reference)obj;
					referenceArray.Append(reference2);
				}
			}
			if (referenceArray.Size < 2)
			{
				return false;
			}
			Dimension dimension = this.doc.Create.NewDimension(this.doc.ActiveView, hyline.Line, referenceArray, this.GetDimTypeByName());
			RevitDimensionExtension.ResetDimTextPos(dimension);
			this.SetDimesionType(dimension);
			if (referenceArray2.Size > 0)
			{
				Dimension dimension2 = this.doc.Create.NewDimension(this.doc.ActiveView, hyline2.Line, referenceArray2, this.GetDimTypeByName());
				RevitDimensionExtension.ResetDimTextPos(dimension2);
				this.SetDimesionType(dimension2);
			}
			this.doc.Regenerate();
			return true;
		}

		private void SetDimesionType(Dimension dimension)
		{
			Parameter parameter = dimension.DimensionType.GetParameter(BuiltInParameter.DIM_WITNS_LINE_CNTRL);
			if (parameter == null)
			{
				return;
			}
			parameter.Set(1);
		}

		public List<Autodesk.Revit.DB.View> GetFloorPlanViews(Autodesk.Revit.DB.Document doc)
		{
			ViewFamilyType floorPlanViewFamilyType = this.GetFloorPlanViewFamilyType(doc);
			List<Autodesk.Revit.DB.View> list = new List<Autodesk.Revit.DB.View>();
			IList<Element> list2 = new FilteredElementCollector(doc).OfClass(typeof(Autodesk.Revit.DB.View)).ToElements();
			if (list2 != null)
			{
				foreach (Element element in list2)
				{
					Autodesk.Revit.DB.View view = element as Autodesk.Revit.DB.View;
					if (view.GetTypeId() == floorPlanViewFamilyType.Id)
					{
						list.Add(view);
					}
				}
			}
			return list;
		}

		public ViewFamilyType GetFloorPlanViewFamilyType(Autodesk.Revit.DB.Document doc)
		{
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc);
			filteredElementCollector.OfClass(typeof(ViewFamilyType));
			return filteredElementCollector.Cast<ViewFamilyType>().First((ViewFamilyType vft) => vft.ViewFamily == 109);
		}

		private List<Reference> GetReferenceList(ReferenceArray array)
		{
			List<Reference> list = new List<Reference>();
			IEnumerator enumerator = array.GetEnumerator();
			enumerator.Reset();
			while (enumerator.MoveNext())
			{
				object obj = enumerator.Current;
				list.Add(obj as Reference);
			}
			return list;
		}

		private void AddEgdeReference(HYLine dimline, Element item, ElementSort sort, ReferenceArray arr, bool isRow, List<double> xyzList = null)
		{
			XYZ xyz = YJKRevitTools.GetDLineP(dimline.StartPoint, dimline.EndPoint, ((LocationCurve)item.Location).Curve.GetEndPoint(0), ((LocationCurve)item.Location).Curve.GetEndPoint(1));
			if (item.Document.IsLinked)
			{
				xyz = this.doc.GetLinkedDocument(item.Id).GetPointInLinkDocument(xyz);
			}
			double radiusOrHeight = sort.GetRadiusOrHeight(item, isRow);
			XYZ xyz2 = 0.005 * this.doc.ActiveView.UpDirection;
			XYZ xyz3 = 0.005 * this.doc.ActiveView.ViewDirection;
			XYZ xyz4 = 0.005 * this.doc.ActiveView.RightDirection;
			if (isRow)
			{
				XYZ xyz5 = xyz + radiusOrHeight * this.doc.ActiveView.RightDirection;
				XYZ xyz6 = xyz5 + xyz2;
				XYZ xyz7 = xyz5 + xyz2 + xyz3;
				ReferencePlane item2 = this.doc.ActiveView.Document.Create.NewReferencePlane2(xyz5, xyz6, xyz7, this.doc.ActiveView);
				this.listHorizontalReference.Add(item2);
				XYZ xyz8 = xyz - radiusOrHeight * this.doc.ActiveView.RightDirection;
				XYZ xyz9 = xyz8 + xyz2;
				XYZ xyz10 = xyz8 + xyz2 + xyz3;
				item2 = this.doc.ActiveView.Document.Create.NewReferencePlane2(xyz8, xyz9, xyz10, this.doc.ActiveView);
				this.doc.Regenerate();
				this.listHorizontalReference.Add(item2);
				return;
			}
			XYZ bubbleEnd2 = xyz - radiusOrHeight * this.doc.ActiveView.UpDirection;
			XYZ xyz11 = bubbleEnd2 + xyz4;
			XYZ xyz12 = bubbleEnd2 + xyz4 + xyz3;
			if (xyzList != null && !(from s in xyzList
			where Math.Abs(s - bubbleEnd2.Z) < 1E-05
			select s).Any<double>())
			{
				ReferencePlane item3 = this.doc.ActiveView.Document.Create.NewReferencePlane2(bubbleEnd2, xyz11, xyz12, this.doc.ActiveView);
				this.doc.Regenerate();
				xyzList.Add(bubbleEnd2.Z);
				this.listVerticalRefernce.Add(item3);
				return;
			}
		}

		private void AddPointReference(HYLine dimline, Element item, ElementSort sort, ReferenceArray arr, bool isRow, List<double> xyzList = null)
		{
			XYZ poistionPoint = YJKRevitTools.GetDLineP(dimline.StartPoint, dimline.EndPoint, ((LocationCurve)item.Location).Curve.GetEndPoint(0), ((LocationCurve)item.Location).Curve.GetEndPoint(1));
			if (item.Document.IsLinked)
			{
				LinkedDocument linkedDocument = this.doc.GetLinkedDocument(item.Id);
				poistionPoint = linkedDocument.GetPointInLinkDocument(poistionPoint);
			}
			sort.GetRadiusOrHeight(item, isRow);
			if (isRow)
			{
				XYZ poistionPoint3 = poistionPoint;
				XYZ xyz = poistionPoint + 0.005 * this.doc.ActiveView.UpDirection;
				XYZ xyz2 = poistionPoint + 0.005 * this.doc.ActiveView.UpDirection + 0.005 * this.doc.ActiveView.ViewDirection;
				ReferencePlane item2 = this.doc.ActiveView.Document.Create.NewReferencePlane2(poistionPoint3, xyz, xyz2, this.doc.ActiveView);
				this.listHorizontalReference.Add(item2);
			}
			else
			{
				DetailCurve detailCurve = this.doc.Create.NewDetailCurve(this.doc.ActiveView, new HYLine(poistionPoint, poistionPoint + 0.005 * this.doc.ActiveView.RightDirection).Line);
				detailCurve.LineStyle = this.doc.GetElement(DefinitionLineType.ReturnLineStyleId(this.doc, detailCurve, "HY-CompLine"));
				if (xyzList == null || (from s in xyzList
				where Math.Abs(s - poistionPoint.Z) < 1E-05
				select s).Any<double>())
				{
					return;
				}
				XYZ poistionPoint2 = poistionPoint;
				XYZ rightDirection = this.doc.ActiveView.RightDirection;
				XYZ viewDirection = this.doc.ActiveView.ViewDirection;
				XYZ xyz3 = poistionPoint + 0.005 * rightDirection;
				XYZ xyz4 = poistionPoint + 0.005 * rightDirection + 0.005 * viewDirection;
				ReferencePlane item2 = this.doc.ActiveView.Document.Create.NewReferencePlane2(poistionPoint2, xyz3, xyz4, this.doc.ActiveView);
				xyzList.Add(poistionPoint.Z);
				this.listVerticalRefernce.Add(item2);
			}
			this.doc.Regenerate();
		}

		private DimensionType GetDimTypeByName()
		{
			int scale = this.doc.ActiveView.Scale;
			string text = "HY-尺寸标注";
			string strMark = "对角线 1.0mm";
			DimensionType dimensionType = null;
			foreach (Element element in new FilteredElementCollector(this.doc).OfClass(typeof(DimensionType)).ToElements())
			{
				if (element.Name == text)
				{
					dimensionType = (element as DimensionType);
					break;
				}
			}
			if (dimensionType == null)
			{
				dimensionType = this.CopyDimensionType(text, strMark);
			}
			return dimensionType;
		}

		private DimensionType CopyDimensionType(string strLabel, string strMark)
		{
			DimensionType result = null;
			string versionNumber = RevitVersion.GetVersionNumber(this.doc);
			string text = Path.Combine(Product.FamilyLocation, versionNumber, "Template", "HYBIMSpace给排水样板.rte");
			Document document = null;
			if (!File.Exists(text))
			{
				return null;
			}
			document = this.doc.Application.OpenDocumentFile(text);
			Element element = null;
			foreach (Element element2 in new FilteredElementCollector(document).OfClass(typeof(DimensionType)).ToElements())
			{
				if (element2.Name == strLabel)
				{
					element = element2;
					break;
				}
			}
			Element element3 = null;
			foreach (Element element4 in new FilteredElementCollector(document).OfClass(typeof(Autodesk.Revit.DB.ElementType)).ToElements())
			{
				if (element4.Name == strMark)
				{
					element3 = element4;
					break;
				}
			}
			if (element == null || element3 == null)
			{
				return null;
			}
			List<ElementId> list = new List<ElementId>();
			list.Add(element.Id);
			List<ElementId> list2 = new List<ElementId>();
			list2.Add(element3.Id);
			SubTransaction subTransaction = new SubTransaction(this.doc);
			subTransaction.Start();
			try
			{
				ICollection<ElementId> collection = ElementTransformUtils.CopyElements(document, list, this.doc, Transform.Identity, new CopyPasteOptions());
				ElementTransformUtils.CopyElements(document, list2, this.doc, Transform.Identity, new CopyPasteOptions());
				subTransaction.Commit();
				if (collection.Count > 0)
				{
					result = (this.doc.GetElement(collection.ElementAt(0)) as DimensionType);
				}
			}
			catch
			{
				subTransaction.RollBack();
			}
			document.Close();
			return result;
		}

		private XYZ CreateElemsSysTypeAndSizeTagging(Autodesk.Revit.DB.Document document, List<Element> operateElems, List<Element> scopeElems, out XYZ leftPoint, out XYZ rightPoint)
		{
			List<Element> list = new List<Element>();
			operateElems = this.GetOperateElems(operateElems);
			list.AddRange(operateElems);
			list.AddRange(scopeElems);
			ElementSort elementSort = new ElementSort(this.doc);
			List<Wall> list2 = new List<Wall>();
			foreach (Element element in list)
			{
				if (element is Wall && (Math.Abs(((Wall)element).Orientation.AngleTo(this.doc.ActiveView.RightDirection)) < 1E-05 || Math.Abs(((Wall)element).Orientation.AngleTo(this.doc.ActiveView.RightDirection) - Math.PI) < 1E-05))
				{
					list2.Add((Wall)element);
				}
			}
			XYZ directionInstance = elementSort.GetDirectionInstance(list, this.doc.ActiveView.RightDirection, false);
			XYZ directionInstance2 = elementSort.GetDirectionInstance(list, -this.doc.ActiveView.RightDirection, false);
			bool leftWall = false;
			bool rightWall = false;
			if (list2.Count > 0)
			{
				this.JudgeBoundary(elementSort, list2, ref directionInstance, ref directionInstance2, ref leftWall, ref rightWall);
			}
			Dictionary<Element, XYZ> positionInSection = this.GetPositionInSection(this.doc, operateElems);
			Dictionary<Element, double> distanceToSectionLeftLine = this.GetDistanceToSectionLeftLine(this.doc, positionInSection);
			XYZ minUpDirectionPoistion = this.GetMinUpDirectionPoistion(document, positionInSection);
			Dictionary<Element, double> dicElemsDistanceSort;
			Dictionary<Element, double> dicElemsDistanceSort2;
			OpearComprhsivPipelineSec.GroupedSchedulingByDistance(distanceToSectionLeftLine, out dicElemsDistanceSort, out dicElemsDistanceSort2);
			Dictionary<Element, XYZ> dictionary = this.CreateTaggingVerticalLine(document, minUpDirectionPoistion, positionInSection, dicElemsDistanceSort);
			Dictionary<Element, XYZ> dictionary2 = this.CreateTaggingVerticalLine(document, minUpDirectionPoistion, positionInSection, dicElemsDistanceSort2);
			CommonFont fontSetting = this.userSetting.GetFontSetting();
			TextNoteType textNoteType = document.GetTextNoteType(fontSetting.FontHeight, fontSetting.FontGWRotia, fontSetting.Font);
			XYZ point = new XYZ();
			XYZ xyz = new XYZ();
			XYZ xyz2 = new XYZ();
			XYZ point2 = new XYZ();
			this.CreateSizeTextNote(document, directionInstance2, dictionary, textNoteType, true, out point, leftWall);
			Dictionary<Element, XYZ> dictionary3 = this.CreateSysTypeTextNote(document, point, dictionary, textNoteType, true, out xyz2, false);
			Dictionary<Element, XYZ> dictionary4 = this.CreateRelativeTextNote(document, xyz2, dictionary, textNoteType, true, out xyz2, false);
			if (dictionary4.Count == 0)
			{
				dictionary4 = dictionary3;
			}
			this.CreateSysTypeTextNote(document, directionInstance, dictionary2, textNoteType, false, out point2, rightWall);
			Dictionary<Element, XYZ> dictionary5 = this.CreateSizeTextNote(document, point2, dictionary2, textNoteType, false, out xyz, false);
			Dictionary<Element, XYZ> dictionary6 = this.CreateRelativeRightTextNote(document, xyz, dictionary2, textNoteType, false, out xyz, false);
			if (dictionary6.Count == 0)
			{
				dictionary6 = dictionary5;
			}
			leftPoint = xyz2;
			rightPoint = xyz;
			this.CreateTaggingHorizontalLine(document, dictionary, dictionary4);
			this.CreateTaggingHorizontalLine(document, dictionary2, dictionary6);
			return (directionInstance + directionInstance2) / 2.0;
		}

		private void JudgeBoundary(ElementSort sort, List<Wall> lists, ref XYZ right, ref XYZ left, ref bool leftWall, ref bool rightWall)
		{
			XYZ xyz = null;
			XYZ xyz2 = null;
			foreach (Wall element in lists)
			{
				XYZ facePoint = sort.GetFacePoint(element, -this.doc.ActiveView.RightDirection);
				if (facePoint != null)
				{
					if (xyz2 != null)
					{
						if (facePoint.Subtract(xyz2).Normalize().AngleTo(-this.doc.ActiveView.RightDirection) <= Math.PI*.5)
						{
							xyz2 = facePoint;
						}
					}
					else
					{
						xyz2 = facePoint;
					}
				}
			}
			foreach (Wall element2 in lists)
			{
				XYZ facePoint2 = sort.GetFacePoint(element2, this.doc.ActiveView.RightDirection);
				if (facePoint2 != null)
				{
					if (xyz != null)
					{
						if (facePoint2.Subtract(xyz).Normalize().AngleTo(this.doc.ActiveView.RightDirection) <= Math.PI*.5)
						{
							xyz = facePoint2;
						}
					}
					else
					{
						xyz = facePoint2;
					}
				}
			}
			if (xyz2 != null && xyz2.Subtract(left).Normalize().AngleTo(-this.doc.ActiveView.RightDirection) <= Math.PI*.5)
			{
				left = xyz2;
				leftWall = true;
			}
			if (xyz != null && xyz.Subtract(right).AngleTo(this.doc.ActiveView.RightDirection) <= Math.PI*.5)
			{
				right = xyz;
				rightWall = true;
			}
		}

		private Dictionary<Element, XYZ> CreateSizeTextNote(Autodesk.Revit.DB.Document document, XYZ point, Dictionary<Element, XYZ> elemsPoint, TextNoteType textNodeType, bool left, out XYZ outPoint, bool leftWall)
		{
			Dictionary<Element, double> dictionary = new Dictionary<Element, double>();
			Dictionary<Element, XYZ> dictionary2 = new Dictionary<Element, XYZ>();
			new Dictionary<Element, double>();
			XYZ xyz = new XYZ();
			foreach (KeyValuePair<Element, XYZ> keyValuePair in elemsPoint)
			{
				XYZ xyz2 = new XYZ(point.X, point.Y, keyValuePair.Value.Z);
				string text;
				string strTextNode;
				this.GetTaggingText(keyValuePair.Key, out text, out strTextNode);
				TextNote textNote;
				using (SubTransaction subTransaction = new SubTransaction(document))
				{
					subTransaction.Start();
					textNote = UnifiedModified.HYCreatTextNote(document, strTextNode, xyz2 + this.MoveTextNoteInDifferentRevitVer(), textNodeType);
					subTransaction.Commit();
				}
				document.Regenerate();
				using (SubTransaction subTransaction2 = new SubTransaction(document))
				{
					double textNoteWidth = UnifiedModified.GetTextNoteWidth(this.doc, textNote);
					double value = (Convert.ToInt32(this.revitVer) >= 2016) ? (textNoteWidth / (double)this.doc.ActiveView.Scale) : (textNoteWidth / ((double)this.doc.ActiveView.Scale / 100.0));
					dictionary.Add(keyValuePair.Key, value);
					subTransaction2.Start();
					document.Delete(textNote.Id);
					subTransaction2.Commit();
				}
			}
			dictionary = (from s in dictionary
			orderby s.Value descending
			select s).ToDictionary((KeyValuePair<Element, double> s) => s.Key, (KeyValuePair<Element, double> s) => s.Value);
			double num = 50.0;
			double num2 = (double)document.ActiveView.GetParameter(BuiltInParameter.VIEW_SCALE).AsInteger();
			double value2 = dictionary.FirstOrDefault<KeyValuePair<Element, double>>().Value;
			if (left)
			{
				if (leftWall)
				{
					xyz = point - (0.32808398950131235 + num2 / num * 50.0 * value2) * document.ActiveView.RightDirection;
					outPoint = xyz;
				}
				else
				{
					xyz = point - (1.6404199475065617 + num2 / num * 50.0 * value2) * document.ActiveView.RightDirection;
					outPoint = xyz;
				}
			}
			else
			{
				xyz = point + 0.32808398950131235 * document.ActiveView.RightDirection;
				outPoint = xyz + num2 / num * 50.0 * value2 * document.ActiveView.RightDirection;
			}
			foreach (KeyValuePair<Element, double> keyValuePair2 in dictionary)
			{
				foreach (KeyValuePair<Element, XYZ> keyValuePair3 in elemsPoint)
				{
					if (keyValuePair2.Key == keyValuePair3.Key)
					{
						XYZ xyz3 = new XYZ(xyz.X, xyz.Y, keyValuePair3.Value.Z);
						string text;
						string strTextNode;
						this.GetTaggingText(keyValuePair3.Key, out text, out strTextNode);
						using (SubTransaction subTransaction3 = new SubTransaction(document))
						{
							subTransaction3.Start();
							UnifiedModified.HYCreatTextNote(document, strTextNode, xyz3 + (this.MoveTextNoteInDifferentRevitVer() + document.ActiveView.UpDirection * 0.65616797900262469) * (num2 / num), textNodeType);
							if (left)
							{
								dictionary2.Add(keyValuePair3.Key, xyz3);
							}
							else
							{
								dictionary2.Add(keyValuePair3.Key, new XYZ(outPoint.X, outPoint.Y, keyValuePair3.Value.Z));
							}
							subTransaction3.Commit();
							break;
						}
					}
				}
			}
			return dictionary2;
		}

		private Dictionary<Element, XYZ> CreateRelativeRightTextNote(Autodesk.Revit.DB.Document document, XYZ point, Dictionary<Element, XYZ> elemsPoint, TextNoteType textNodeType, bool left, out XYZ outPoint, bool leftWall)
		{
			Dictionary<Element, double> dictionary = new Dictionary<Element, double>();
			Dictionary<Element, XYZ> dictionary2 = new Dictionary<Element, XYZ>();
			new Dictionary<Element, double>();
			XYZ xyz = new XYZ();
			foreach (KeyValuePair<Element, XYZ> keyValuePair in elemsPoint)
			{
				XYZ xyz2 = new XYZ(point.X, point.Y, keyValuePair.Value.Z);
				string elemRelativeLevel = this.GetElemRelativeLevel(keyValuePair.Key);
				if (!string.IsNullOrEmpty(elemRelativeLevel))
				{
					TextNote textNote;
					using (SubTransaction subTransaction = new SubTransaction(document))
					{
						subTransaction.Start();
						textNote = UnifiedModified.HYCreatTextNote(document, elemRelativeLevel, xyz2 + this.MoveTextNoteInDifferentRevitVer(), textNodeType);
						subTransaction.Commit();
					}
					document.Regenerate();
					using (SubTransaction subTransaction2 = new SubTransaction(document))
					{
						double textNoteWidth = UnifiedModified.GetTextNoteWidth(this.doc, textNote);
						double value = (Convert.ToInt32(this.revitVer) >= 2016) ? (textNoteWidth / (double)this.doc.ActiveView.Scale) : (textNoteWidth / ((double)this.doc.ActiveView.Scale / 100.0));
						dictionary.Add(keyValuePair.Key, value);
						subTransaction2.Start();
						document.Delete(textNote.Id);
						subTransaction2.Commit();
					}
				}
			}
			dictionary = (from s in dictionary
			orderby s.Value descending
			select s).ToDictionary((KeyValuePair<Element, double> s) => s.Key, (KeyValuePair<Element, double> s) => s.Value);
			double num = 50.0;
			double num2 = (double)document.ActiveView.GetParameter(BuiltInParameter.VIEW_SCALE).AsInteger();
			double value2 = dictionary.FirstOrDefault<KeyValuePair<Element, double>>().Value;
			if (left)
			{
				if (leftWall)
				{
					xyz = point - (0.32808398950131235 + num2 / num * 50.0 * value2) * document.ActiveView.RightDirection;
					outPoint = xyz;
				}
				else
				{
					xyz = point - (1.6404199475065617 + num2 / num * 50.0 * value2) * document.ActiveView.RightDirection;
					outPoint = xyz;
				}
			}
			else
			{
				xyz = point + 0.32808398950131235 * document.ActiveView.RightDirection;
				outPoint = xyz + num2 / num * 50.0 * value2 * document.ActiveView.RightDirection;
			}
			foreach (KeyValuePair<Element, double> keyValuePair2 in dictionary)
			{
				foreach (KeyValuePair<Element, XYZ> keyValuePair3 in elemsPoint)
				{
					if (keyValuePair2.Key == keyValuePair3.Key)
					{
						XYZ xyz3 = new XYZ(xyz.X, xyz.Y, keyValuePair3.Value.Z);
						string elemRelativeLevel = this.GetElemRelativeLevel(keyValuePair3.Key);
						if (!string.IsNullOrEmpty(elemRelativeLevel))
						{
							using (SubTransaction subTransaction3 = new SubTransaction(document))
							{
								subTransaction3.Start();
								UnifiedModified.HYCreatTextNote(document, elemRelativeLevel, xyz3 + (this.MoveTextNoteInDifferentRevitVer() + document.ActiveView.UpDirection * 0.65616797900262469) * (num2 / num), textNodeType);
								if (left)
								{
									dictionary2.Add(keyValuePair3.Key, xyz3);
								}
								else
								{
									dictionary2.Add(keyValuePair3.Key, new XYZ(outPoint.X, outPoint.Y, keyValuePair3.Value.Z));
								}
								subTransaction3.Commit();
								break;
							}
						}
					}
				}
			}
			return dictionary2;
		}

		private Dictionary<Element, XYZ> CreateSysTypeTextNote(Autodesk.Revit.DB.Document document, XYZ point, Dictionary<Element, XYZ> elemsPoint, TextNoteType textNodeType, bool left, out XYZ outPoint, bool rightWall)
		{
			Dictionary<Element, double> dictionary = new Dictionary<Element, double>();
			Dictionary<Element, XYZ> dictionary2 = new Dictionary<Element, XYZ>();
			XYZ xyz = new XYZ();
			foreach (KeyValuePair<Element, XYZ> keyValuePair in elemsPoint)
			{
				XYZ xyz2 = new XYZ(point.X, point.Y, keyValuePair.Value.Z);
				string strTextNode;
				string text;
				this.GetTaggingText(keyValuePair.Key, out strTextNode, out text);
				TextNote textNote;
				using (SubTransaction subTransaction = new SubTransaction(document))
				{
					subTransaction.Start();
					textNote = UnifiedModified.HYCreatTextNote(document, strTextNode, xyz2 + this.MoveTextNoteInDifferentRevitVer(), textNodeType);
					subTransaction.Commit();
				}
				document.Regenerate();
				using (SubTransaction subTransaction2 = new SubTransaction(document))
				{
					double textNoteWidth = UnifiedModified.GetTextNoteWidth(this.doc, textNote);
					double value = (Convert.ToInt32(this.revitVer) >= 2016) ? (textNoteWidth / (double)this.doc.ActiveView.Scale) : (textNoteWidth / ((double)this.doc.ActiveView.Scale / 100.0));
					dictionary.Add(keyValuePair.Key, value);
					subTransaction2.Start();
					document.Delete(textNote.Id);
					subTransaction2.Commit();
				}
			}
			dictionary = (from s in dictionary
			orderby s.Value descending
			select s).ToDictionary((KeyValuePair<Element, double> s) => s.Key, (KeyValuePair<Element, double> s) => s.Value);
			double num = 50.0;
			double num2 = (double)document.ActiveView.GetParameter(BuiltInParameter.VIEW_SCALE).AsInteger();
			double value2 = dictionary.FirstOrDefault<KeyValuePair<Element, double>>().Value;
			if (left)
			{
				outPoint = point - (0.32808398950131235 + num2 / num * 50.0 * value2) * document.ActiveView.RightDirection;
			}
			else if (rightWall)
			{
				outPoint = point + (0.32808398950131235 + num2 / num * 50.0 * value2) * document.ActiveView.RightDirection;
			}
			else
			{
				outPoint = point + (1.6404199475065617 + num2 / num * 50.0 * value2) * document.ActiveView.RightDirection;
			}
			foreach (KeyValuePair<Element, double> keyValuePair2 in dictionary)
			{
				if (left)
				{
					xyz = point - (0.32808398950131235 + num2 / num * 50.0 * keyValuePair2.Value) * document.ActiveView.RightDirection;
				}
				else if (rightWall)
				{
					xyz = point + (0.32808398950131235 + num2 / num * 50.0 * (value2 - keyValuePair2.Value)) * document.ActiveView.RightDirection;
				}
				else
				{
					xyz = point + (1.6404199475065617 + num2 / num * 50.0 * (value2 - keyValuePair2.Value)) * document.ActiveView.RightDirection;
				}
				foreach (KeyValuePair<Element, XYZ> keyValuePair3 in elemsPoint)
				{
					if (keyValuePair2.Key == keyValuePair3.Key)
					{
						XYZ xyz3 = new XYZ(xyz.X, xyz.Y, keyValuePair3.Value.Z);
						string strTextNode;
						string text;
						this.GetTaggingText(keyValuePair2.Key, out strTextNode, out text);
						using (SubTransaction subTransaction3 = new SubTransaction(document))
						{
							subTransaction3.Start();
							UnifiedModified.HYCreatTextNote(document, strTextNode, xyz3 + (this.MoveTextNoteInDifferentRevitVer() + document.ActiveView.UpDirection * 0.65616797900262469) * (num2 / num), textNodeType);
							if (left)
							{
								dictionary2.Add(keyValuePair2.Key, xyz3);
							}
							else
							{
								dictionary2.Add(keyValuePair2.Key, xyz3 + 50.0 * keyValuePair2.Value * document.ActiveView.RightDirection);
							}
							subTransaction3.Commit();
							break;
						}
					}
				}
			}
			return dictionary2;
		}

		private Dictionary<Element, XYZ> CreateRelativeTextNote(Autodesk.Revit.DB.Document document, XYZ point, Dictionary<Element, XYZ> elemsPoint, TextNoteType textNodeType, bool left, out XYZ outPoint, bool rightWall)
		{
			Dictionary<Element, double> dictionary = new Dictionary<Element, double>();
			Dictionary<Element, XYZ> dictionary2 = new Dictionary<Element, XYZ>();
			XYZ xyz = new XYZ();
			foreach (KeyValuePair<Element, XYZ> keyValuePair in elemsPoint)
			{
				XYZ xyz2 = new XYZ(point.X, point.Y, keyValuePair.Value.Z);
				string elemRelativeLevel = this.GetElemRelativeLevel(keyValuePair.Key);
				if (!string.IsNullOrEmpty(elemRelativeLevel))
				{
					TextNote textNote;
					using (SubTransaction subTransaction = new SubTransaction(document))
					{
						subTransaction.Start();
						textNote = UnifiedModified.HYCreatTextNote(document, elemRelativeLevel, xyz2 + this.MoveTextNoteInDifferentRevitVer(), textNodeType);
						subTransaction.Commit();
					}
					document.Regenerate();
					using (SubTransaction subTransaction2 = new SubTransaction(document))
					{
						double textNoteWidth = UnifiedModified.GetTextNoteWidth(this.doc, textNote);
						double value = (Convert.ToInt32(this.revitVer) >= 2016) ? (textNoteWidth / (double)this.doc.ActiveView.Scale) : (textNoteWidth / ((double)this.doc.ActiveView.Scale / 100.0));
						dictionary.Add(keyValuePair.Key, value);
						subTransaction2.Start();
						document.Delete(textNote.Id);
						subTransaction2.Commit();
					}
				}
			}
			dictionary = (from s in dictionary
			orderby s.Value descending
			select s).ToDictionary((KeyValuePair<Element, double> s) => s.Key, (KeyValuePair<Element, double> s) => s.Value);
			double num = 50.0;
			double num2 = (double)document.ActiveView.GetParameter(BuiltInParameter.VIEW_SCALE).AsInteger();
			double value2 = dictionary.FirstOrDefault<KeyValuePair<Element, double>>().Value;
			if (left)
			{
				outPoint = point - (0.32808398950131235 + num2 / num * 50.0 * value2) * document.ActiveView.RightDirection;
			}
			else if (rightWall)
			{
				outPoint = point + (0.32808398950131235 + num2 / num * 50.0 * value2) * document.ActiveView.RightDirection;
			}
			else
			{
				outPoint = point + (0.32808398950131235 + num2 / num * 50.0 * value2) * document.ActiveView.RightDirection;
			}
			foreach (KeyValuePair<Element, double> keyValuePair2 in dictionary)
			{
				if (left)
				{
					xyz = point - (0.32808398950131235 + num2 / num * 50.0 * keyValuePair2.Value) * document.ActiveView.RightDirection;
				}
				else if (rightWall)
				{
					xyz = point + (0.32808398950131235 + num2 / num * 50.0 * (value2 - keyValuePair2.Value)) * document.ActiveView.RightDirection;
				}
				else
				{
					xyz = point + (0.32808398950131235 + num2 / num * 50.0 * (value2 - keyValuePair2.Value)) * document.ActiveView.RightDirection;
				}
				foreach (KeyValuePair<Element, XYZ> keyValuePair3 in elemsPoint)
				{
					if (keyValuePair2.Key == keyValuePair3.Key)
					{
						XYZ xyz3 = new XYZ(xyz.X, xyz.Y, keyValuePair3.Value.Z);
						string elemRelativeLevel = this.GetElemRelativeLevel(keyValuePair2.Key);
						if (!string.IsNullOrEmpty(elemRelativeLevel))
						{
							using (SubTransaction subTransaction3 = new SubTransaction(document))
							{
								subTransaction3.Start();
								UnifiedModified.HYCreatTextNote(document, elemRelativeLevel, xyz3 + (this.MoveTextNoteInDifferentRevitVer() + document.ActiveView.UpDirection * 0.65616797900262469) * (num2 / num), textNodeType);
								if (left)
								{
									dictionary2.Add(keyValuePair2.Key, xyz3);
								}
								else
								{
									dictionary2.Add(keyValuePair2.Key, xyz3 + 50.0 * keyValuePair2.Value * document.ActiveView.RightDirection);
								}
								subTransaction3.Commit();
								break;
							}
						}
					}
				}
			}
			return dictionary2;
		}

		private Dictionary<Element, XYZ> CreateTaggingVerticalLine(Autodesk.Revit.DB.Document document, XYZ minUpDirectionPoistion, Dictionary<Element, XYZ> dicElmesPoistion, Dictionary<Element, double> dicElemsDistanceSort)
		{
			Dictionary<Element, XYZ> dictionary = new Dictionary<Element, XYZ>();
			double num = 50.0;
			double num2 = (double)document.ActiveView.GetParameter(BuiltInParameter.VIEW_SCALE).AsInteger();
			int num3 = 0;
			foreach (KeyValuePair<Element, double> keyValuePair in dicElemsDistanceSort)
			{
				foreach (KeyValuePair<Element, XYZ> keyValuePair2 in dicElmesPoistion)
				{
					if (keyValuePair.Key == keyValuePair2.Key)
					{
						XYZ value = keyValuePair2.Value;
						XYZ xyz = value - document.ActiveView.UpDirection * ((double)num3 * 0.85301837270341208 * (num2 / num) + num2 / num * 300.0 / 304.8 + value.Z - minUpDirectionPoistion.Z);
						using (SubTransaction subTransaction = new SubTransaction(document))
						{
							subTransaction.Start();
							DetailCurve detailCurve = document.Create.NewDetailCurve(document.ActiveView, Line.CreateBound(value, xyz));
							detailCurve.LineStyle = this.doc.GetElement(DefinitionLineType.ReturnLineStyleId(this.doc, detailCurve, "HY-CompLine"));
							subTransaction.Commit();
						}
						dictionary.Add(keyValuePair.Key, xyz);
						num3++;
						break;
					}
				}
			}
			return dictionary;
		}

		private void CreateTaggingHorizontalLine(Autodesk.Revit.DB.Document document, Dictionary<Element, XYZ> elemsStartPoint, Dictionary<Element, XYZ> elemsEndPoint)
		{
			foreach (KeyValuePair<Element, XYZ> keyValuePair in elemsStartPoint)
			{
				foreach (KeyValuePair<Element, XYZ> keyValuePair2 in elemsEndPoint)
				{
					if (keyValuePair.Key == keyValuePair2.Key)
					{
						using (SubTransaction subTransaction = new SubTransaction(document))
						{
							subTransaction.Start();
							try
							{
								DetailCurve detailCurve = document.Create.NewDetailCurve(document.ActiveView, Line.CreateBound(keyValuePair.Value, keyValuePair2.Value));
								detailCurve.LineStyle = this.doc.GetElement(DefinitionLineType.ReturnLineStyleId(this.doc, detailCurve, "HY-CompLine"));
							}
							catch (Exception)
							{
							}
							subTransaction.Commit();
							break;
						}
					}
				}
			}
		}

		private XYZ GetMinUpDirectionPoistion(Autodesk.Revit.DB.Document document, Dictionary<Element, XYZ> dicElmesPoistion)
		{
			XYZ xyz = null;
			foreach (KeyValuePair<Element, XYZ> keyValuePair in dicElmesPoistion)
			{
				if (xyz == null)
				{
					xyz = keyValuePair.Value;
				}
				if (xyz.Z > keyValuePair.Value.Z)
				{
					xyz = keyValuePair.Value;
				}
			}
			return xyz;
		}

		private static void GroupedSchedulingByDistance(Dictionary<Element, double> dicElemsDistance, out Dictionary<Element, double> dicLeftElemsAscendingSort, out Dictionary<Element, double> dicRightElemsDescendingSort)
		{
			double num = dicElemsDistance.Min((KeyValuePair<Element, double> c) => c.Value);
			double num2 = dicElemsDistance.Max((KeyValuePair<Element, double> c) => c.Value);
			double num3 = (num + num2) / 2.0;
			Dictionary<Element, double> dictionary = new Dictionary<Element, double>();
			Dictionary<Element, double> dictionary2 = new Dictionary<Element, double>();
			foreach (KeyValuePair<Element, double> keyValuePair in dicElemsDistance)
			{
				if (keyValuePair.Value > num3)
				{
					dictionary2.Add(keyValuePair.Key, keyValuePair.Value);
				}
				else if (keyValuePair.Value < num3)
				{
					dictionary.Add(keyValuePair.Key, keyValuePair.Value);
				}
			}
			if (dicElemsDistance.Count > 0)
			{
				if (dictionary.Count < dictionary2.Count)
				{
					using (Dictionary<Element, double>.Enumerator enumerator = dicElemsDistance.GetEnumerator())
					{
						while (enumerator.MoveNext())
						{
							KeyValuePair<Element, double> keyValuePair2 = enumerator.Current;
							if (keyValuePair2.Value == num3)
							{
								dictionary.Add(keyValuePair2.Key, keyValuePair2.Value);
							}
						}
						goto IL_17C;
					}
				}
				foreach (KeyValuePair<Element, double> keyValuePair3 in dicElemsDistance)
				{
					if (keyValuePair3.Value == num3)
					{
						dictionary2.Add(keyValuePair3.Key, keyValuePair3.Value);
					}
				}
			}
			IL_17C:
			dicLeftElemsAscendingSort = (from s in dictionary
			orderby s.Value
			select s).ToDictionary((KeyValuePair<Element, double> s) => s.Key, (KeyValuePair<Element, double> s) => s.Value);
			dicRightElemsDescendingSort = (from s in dictionary2
			orderby s.Value descending
			select s).ToDictionary((KeyValuePair<Element, double> s) => s.Key, (KeyValuePair<Element, double> s) => s.Value);
		}

		public List<Element> GetElemsOfComprhsivPipeline(Autodesk.Revit.DB.Document document, bool operateElems)
		{
			List<Element> list = new List<Element>();
			List<Element> list2 = new List<Element>();
			List<Element> collection = new List<Element>();
			list2 = new FilteredElementCollector(document, document.ActiveView.Id).OfCategory(BuiltInCategory.OST_RvtLinks).WhereElementIsNotElementType().ToElements().ToList<Element>();
			Transform transform = ((ViewSection)this.doc.ActiveView).get_BoundingBox(this.doc.ActiveView).Transform;
			BoundingBoxXYZ boundingBoxXYZ = this.doc.ActiveView.get_BoundingBox(this.doc.ActiveView);
			XYZ xyz = transform.OfPoint(boundingBoxXYZ.Min);
			XYZ xyz2 = transform.OfPoint(boundingBoxXYZ.Max);
			XYZ xyz3 = new XYZ((xyz.X < xyz2.X) ? xyz.X : xyz2.X, (xyz.Y < xyz2.Y) ? xyz.Y : xyz2.Y, (xyz.Z < xyz2.Z) ? xyz.Z : xyz2.Z);
			XYZ xyz4 = new XYZ((xyz.X > xyz2.X) ? xyz.X : xyz2.X, (xyz.Y > xyz2.Y) ? xyz.Y : xyz2.Y, (xyz.Z > xyz2.Z) ? xyz.Z : xyz2.Z);
			BoundingBoxIntersectsFilter item = new BoundingBoxIntersectsFilter(new Outline(xyz3, xyz4));
			ElementCategoryFilter item2 = new ElementCategoryFilter(BuiltInCategory.OST_PipeCurves);
			ElementCategoryFilter item3 = new ElementCategoryFilter(BuiltInCategory.OST_DuctCurves);
			ElementCategoryFilter item4 = new ElementCategoryFilter(BuiltInCategory.OST_CableTray);
			ElementCategoryFilter item5 = new ElementCategoryFilter(BuiltInCategory.OST_Conduit);
			ElementCategoryFilter item6 = new ElementCategoryFilter(BuiltInCategory.OST_Walls);
			ElementCategoryFilter item7 = new ElementCategoryFilter(BuiltInCategory.OST_Floors);
			ElementCategoryFilter item8 = new ElementCategoryFilter(BuiltInCategory.OST_Ceilings);
			ElementCategoryFilter item9 = new ElementCategoryFilter(BuiltInCategory.OST_Levels);
			IList<ElementFilter> list3;
			if (operateElems)
			{
				list3 = new List<ElementFilter>
				{
					item2,
					item3,
					item4,
					item5
				};
			}
			else
			{
				list3 = new List<ElementFilter>
				{
					item6,
					item7,
					item8,
					item9
				};
			}
			LogicalOrFilter item10 = new LogicalOrFilter(list3);
			LogicalAndFilter logicalAndFilter = new LogicalAndFilter(new List<ElementFilter>
			{
				item10,
				item
			});
			collection = new FilteredElementCollector(document, document.ActiveView.Id).WherePasses(logicalAndFilter).WhereElementIsNotElementType().ToElements().ToList<Element>();
			list.AddRange(collection);
			if (list2.Count > 0)
			{
				List<Element> list4 = new List<Element>();
				foreach (Element element in list2)
				{
					list4.Clear();
					list4 = new FilteredElementCollector((element as RevitLinkInstance).GetLinkDocument()).WherePasses(logicalAndFilter).WhereElementIsNotElementType().ToElements().ToList<Element>();
					list.AddRange(list4);
				}
			}
			if (operateElems)
			{
				return this.FilterElemsVerticalSec(document, list);
			}
			return list;
		}

		private List<Element> FilterOperateElemsInActiveView(Autodesk.Revit.DB.Document document, bool isLinkDoc, ElementId id = null)
		{
			FilteredElementCollector filteredElementCollector;
			if (isLinkDoc)
			{
				filteredElementCollector = new FilteredElementCollector(document);
			}
			else
			{
				filteredElementCollector = new FilteredElementCollector(document, document.ActiveView.Id);
			}
			ElementCategoryFilter item = new ElementCategoryFilter(BuiltInCategory.OST_PipeCurves);
			ElementCategoryFilter item2 = new ElementCategoryFilter(BuiltInCategory.OST_DuctCurves);
			ElementCategoryFilter item3 = new ElementCategoryFilter(BuiltInCategory.OST_CableTray);
			ElementCategoryFilter item4 = new ElementCategoryFilter(BuiltInCategory.OST_Conduit);
			LogicalOrFilter logicalOrFilter = new LogicalOrFilter(new List<ElementFilter>
			{
				item,
				item2,
				item3,
				item4
			});
			return filteredElementCollector.WherePasses(logicalOrFilter).WhereElementIsNotElementType().ToElements().ToList<Element>();
		}

		private List<Element> FilterScopeElemsInActiveView(Autodesk.Revit.DB.Document document, bool isLinkDoc)
		{
			FilteredElementCollector filteredElementCollector;
			if (isLinkDoc)
			{
				filteredElementCollector = new FilteredElementCollector(document);
			}
			else
			{
				filteredElementCollector = new FilteredElementCollector(document, document.ActiveView.Id);
			}
			ElementCategoryFilter item = new ElementCategoryFilter(BuiltInCategory.OST_Walls);
			ElementCategoryFilter item2 = new ElementCategoryFilter(BuiltInCategory.OST_Floors);
			ElementCategoryFilter item3 = new ElementCategoryFilter(BuiltInCategory.OST_Ceilings);
			ElementCategoryFilter item4 = new ElementCategoryFilter(BuiltInCategory.OST_Levels);
			LogicalOrFilter logicalOrFilter = new LogicalOrFilter(new List<ElementFilter>
			{
				item,
				item2,
				item3,
				item4
			});
			return filteredElementCollector.WherePasses(logicalOrFilter).WhereElementIsNotElementType().ToElements().ToList<Element>();
		}

		private List<Element> FilterElemsVerticalSec(Autodesk.Revit.DB.Document doc, List<Element> elems)
		{
			List<Element> list = new List<Element>();
			foreach (Element element in elems)
			{
				XYZ xyz = ((LocationCurve)element.Location).Curve.GetEndPoint(0) - ((LocationCurve)element.Location).Curve.GetEndPoint(1);
				if (Math.Abs(xyz.Normalize().AngleTo(doc.ActiveView.ViewDirection)) <= 0.1 || Math.Abs(xyz.Normalize().AngleTo(doc.ActiveView.ViewDirection) - Math.PI) <= 0.1)
				{
					list.Add(element);
				}
			}
			return list;
		}

		private Dictionary<Element, XYZ> GetPositionInSection(Autodesk.Revit.DB.Document document, List<Element> elems)
		{
			Dictionary<Element, XYZ> dictionary = new Dictionary<Element, XYZ>();
			HYLine hyline = new HYLine(document.ActiveView.Origin, document.ActiveView.Origin + document.ActiveView.RightDirection);
			hyline.MakeUnBound();
			foreach (Element element in elems)
			{
				XYZ dlineP = YJKRevitTools.GetDLineP(hyline.StartPoint, hyline.EndPoint, ((LocationCurve)element.Location).Curve.GetEndPoint(0), ((LocationCurve)element.Location).Curve.GetEndPoint(1));
				if (element.Document.IsLinked)
				{
					XYZ pointInLinkDocument = document.GetLinkedDocument(element.Id).GetPointInLinkDocument(dlineP);
					dictionary.Add(element, pointInLinkDocument);
				}
				else
				{
					dictionary.Add(element, dlineP);
				}
			}
			return dictionary;
		}

		private Dictionary<Element, double> GetDistanceToSectionLeftLine(Autodesk.Revit.DB.Document document, Dictionary<Element, XYZ> dicElmesPoistion)
		{
			Dictionary<Element, double> dictionary = new Dictionary<Element, double>();
			Dictionary<Element, double> dictionary2 = new Dictionary<Element, double>();
			HYLine hyline = new HYLine(document.ActiveView.Origin, document.ActiveView.Origin + document.ActiveView.UpDirection);
			hyline.MakeUnBound();
			foreach (KeyValuePair<Element, XYZ> keyValuePair in dicElmesPoistion)
			{
				Element key = keyValuePair.Key;
				double distance = hyline.GetDistance(keyValuePair.Value);
				dictionary2.Add(key, distance);
			}
			dictionary2 = (from s in dictionary2
			orderby s.Value descending
			select s).ToDictionary((KeyValuePair<Element, double> s) => s.Key, (KeyValuePair<Element, double> s) => s.Value);
			KeyValuePair<Element, double> keyValuePair2 = dictionary2.ElementAt(0);
			XYZ start = document.ActiveView.Origin - document.ActiveView.RightDirection * keyValuePair2.Value;
			XYZ end = document.ActiveView.Origin - document.ActiveView.RightDirection * keyValuePair2.Value + document.ActiveView.UpDirection;
			HYLine hyline2 = new HYLine(start, end);
			hyline2.MakeUnBound();
			foreach (KeyValuePair<Element, XYZ> keyValuePair3 in dicElmesPoistion)
			{
				Element key2 = keyValuePair3.Key;
				double distance2 = hyline2.GetDistance(keyValuePair3.Value);
				dictionary.Add(key2, distance2);
			}
			return dictionary;
		}

		private void GetTaggingText(Element elem, out string typeTagging, out string sizeTagging)
		{
			string text = "";
			string text2 = "";
			char[] trimChars = new char[]
			{
				'm'
			};
			if (elem is Pipe)
			{
				text = elem.GetParameter(BuiltInParameter.RBS_PIPING_SYSTEM_TYPE_PARAM).AsValueString().Trim();
				if (!text.Contains("系统"))
				{
					text += "系统";
				}
				text2 = text2 + "DN" + elem.GetParameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).AsValueString().Trim().Trim(trimChars);
			}
			else if (elem is CableTray)
			{
				text = (elem as CableTray).GetParameter(BuiltInParameter.ELEM_FAMILY_AND_TYPE_PARAM).Element.Name;
				text2 += elem.GetParameter(BuiltInParameter.RBS_CABLETRAY_WIDTH_PARAM).AsValueString().Trim(trimChars).Trim();
				text2 = text2 + "×" + elem.GetParameter(BuiltInParameter.RBS_CABLETRAY_HEIGHT_PARAM).AsValueString().Trim(trimChars);
			}
			else if (elem is Duct)
			{
				text = elem.GetParameter(BuiltInParameter.RBS_DUCT_SYSTEM_TYPE_PARAM).AsValueString().Trim();
				if (!text.Contains("系统"))
				{
					text += "系统";
				}
				Parameter parameter = elem.GetParameter(BuiltInParameter.RBS_CURVE_WIDTH_PARAM);
				Parameter parameter2 = elem.GetParameter(BuiltInParameter.RBS_CURVE_DIAMETER_PARAM);
				if (parameter != null)
				{
					text2 += parameter.AsValueString().Trim(trimChars).Trim();
					text2 = text2 + "×" + elem.GetParameter(BuiltInParameter.RBS_CURVE_HEIGHT_PARAM).AsValueString().Trim(trimChars).Trim();
				}
				else if (parameter2 != null)
				{
					text2 = text2 + "DN" + parameter2.AsValueString().Trim().Trim(trimChars);
				}
			}
			else if (elem is Conduit)
			{
				text = elem.GetParameter(BuiltInParameter.RBS_CTC_SERVICE_TYPE).AsString();
				text = ((text == null) ? null : text.ToString());
				if (text != null && !text.Contains("系统"))
				{
					text += "系统";
				}
				text2 = text2 + "DN" + elem.GetParameter(BuiltInParameter.RBS_CONDUIT_DIAMETER_PARAM).AsValueString().Trim().Trim(trimChars);
			}
			typeTagging = ((text == null) ? elem.GetParameter(BuiltInParameter.ELEM_CATEGORY_PARAM).AsValueString() : text);
			sizeTagging = text2;
		}

		private string GetElemRelativeLevel(Element elem)
		{
			string text = "";
			if (!this.tubeSynthesize.BeOffset)
			{
				return text;
			}
			Parameter parameter = elem.GetParameter(BuiltInParameter.RBS_OFFSET_PARAM);
			if (parameter != null && parameter.HasValue)
			{
				text = text + "H+" + (parameter.AsDouble() * 0.3048).ToString("f3");
			}
			return text;
		}

		public TextNoteType GetTextNoteTypeByName(Autodesk.Revit.DB.Document document, string name)
		{
			TextNoteType textNoteType = null;
			foreach (Element element in new FilteredElementCollector(document).OfClass(typeof(TextNoteType)).ToElements())
			{
				TextNoteType textNoteType2 = (TextNoteType)element;
				if (textNoteType2.Name.Equals(name))
				{
					textNoteType = textNoteType2;
					break;
				}
			}
			if (textNoteType == null)
			{
				textNoteType = (this.CopyTypeFromTemplate(document, typeof(TextNoteType), name, "") as TextNoteType);
			}
			return textNoteType;
		}

		public Element CopyTypeFromTemplate(Autodesk.Revit.DB.Document doc, Type targetType, string strLabel, string strMark)
		{
			Element result = null;
			string versionNumber = RevitVersion.GetVersionNumber(doc);
			string text = Path.Combine(Product.FamilyLocation, versionNumber, "Template", "HYBIMSpace给排水样板.rte");
			Document document = null;
			if (!File.Exists(text))
			{
				return null;
			}
			document = doc.Application.OpenDocumentFile(text);
			Element element = null;
			foreach (Element element2 in new FilteredElementCollector(document).OfClass(targetType).ToElements())
			{
				if (element2.Name == strLabel)
				{
					element = element2;
					break;
				}
			}
			Element element3 = null;
			if (strMark.Trim() != "")
			{
				foreach (Element element4 in new FilteredElementCollector(document).OfClass(typeof(Autodesk.Revit.DB.ElementType)).ToElements())
				{
					if (element4.Name == strMark)
					{
						element3 = element4;
						break;
					}
				}
			}
			if (element == null && element3 == null)
			{
				return null;
			}
			if (element3 != null)
			{
				List<ElementId> list = new List<ElementId>();
				list.Add(element.Id);
				List<ElementId> list2 = new List<ElementId>();
				list2.Add(element3.Id);
				SubTransaction subTransaction = new SubTransaction(doc);
				subTransaction.Start();
				try
				{
					ICollection<ElementId> collection = ElementTransformUtils.CopyElements(document, list, doc, Transform.Identity, new CopyPasteOptions());
					ElementTransformUtils.CopyElements(document, list2, doc, Transform.Identity, new CopyPasteOptions());
					subTransaction.Commit();
					if (collection.Count > 0)
					{
						result = doc.GetElement(collection.ElementAt(0));
					}
					goto IL_209;
				}
				catch
				{
					subTransaction.RollBack();
					goto IL_209;
				}
			}
			List<ElementId> list3 = new List<ElementId>();
			list3.Add(element.Id);
			SubTransaction subTransaction2 = new SubTransaction(doc);
			subTransaction2.Start();
			try
			{
				ICollection<ElementId> collection2 = ElementTransformUtils.CopyElements(document, list3, doc, Transform.Identity, new CopyPasteOptions());
				subTransaction2.Commit();
				if (collection2.Count > 0)
				{
					result = doc.GetElement(collection2.ElementAt(0));
				}
			}
			catch
			{
				subTransaction2.RollBack();
			}
			IL_209:
			document.Close();
			return result;
		}

		private XYZ MoveTextNoteInDifferentRevitVer()
		{
			XYZ result = new XYZ();
			string a = this.revitVer;
			if (!(a == "2018") && !(a == "2017"))
			{
				if (!(a == "2016"))
				{
					if (a == "2015")
					{
						result = -0.32808398950131235 * this.doc.ActiveView.UpDirection;
					}
				}
				else
				{
					result = new XYZ(0.0, 0.0, 0.0);
				}
			}
			else
			{
				result = 0.32808398950131235 * this.doc.ActiveView.UpDirection;
			}
			return result;
		}

		private Document doc;

		private double innerDistance = 50.0;

		private double outerDistance = 250.0;

		private string revitVer = "";

		private UserSetting userSetting = new UserSetting();

		private TubeSynthesize tubeSynthesize;

		private List<ReferencePlane> listHorizontalReference = new List<ReferencePlane>();

		private List<ReferencePlane> listVerticalRefernce = new List<ReferencePlane>();
	}
}
